package com.luoke.service.response.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.luoke.connect.GameWebSocketClient;
import com.luoke.constants.ModuleTypeConstant;
import com.luoke.constants.SubmoduleTypeConstant;
import com.luoke.context.UserContext;
import com.luoke.handler.request.BattleRequestHandler;
import com.luoke.pojo.dto.BagDTO;
import com.luoke.pojo.dto.BattleRoundDTO;
import com.luoke.pojo.entity.BattlePet;
import com.luoke.pojo.entity.Skill;
import com.luoke.result.Result;
import com.luoke.service.request.BattleRequestService;
import com.luoke.service.request.impl.BattleRequestServiceImpl;
import com.luoke.service.response.BattleRoundResponseService;

import java.util.List;
import java.util.Scanner;

public class BattleRoundResponseServiceImpl implements BattleRoundResponseService {

    private BattleRequestService battleRequestService = new BattleRequestServiceImpl();
    /**
     * 对服务端发来的先手回合响应进行处理
     *
     * @param gameWebSocketClient 对服务端的连接对象
     * @param result              响应结果
     */
    @Override
    public void firstHandResponse(GameWebSocketClient gameWebSocketClient, Result result) {
        if (UserContext.getCurrentUser().equals(result.getData().toString())) {
            System.out.println(result.getMessage());
        } else {
            BattleRoundDTO data = JSONUtil.toBean(JSONUtil.toJsonStr(result.getData()),BattleRoundDTO.class);
            System.out.println(result.getMessage());
            showBattleMenu(gameWebSocketClient,data);  //先手
        }

    }

    /**
     * 用于每回合的对战信息的展示
     * @param gameWebSocketClient
     * @param data
     */
    private void showBattleMenu(GameWebSocketClient gameWebSocketClient, BattleRoundDTO data) {
        System.out.println("---------------宠物对战message ("+UserContext.getCurrentUser()+" )--------------");
        System.out.println(StrUtil.format("{}                    {}", data.getInitiatorName(), data.getOpponentName()));
        System.out.println(StrUtil.format("{}(属性:{})                    {}(属性:{})", data.getInitiatorPetName(),data.getInitiatorPetMain(), data.getOpponentPetName(),data.getOpponentPetMain()));
        System.out.println(StrUtil.format("lv.{}                    lv.{}", data.getInitiatorPetLevel(), data.getOpponentPetLevel()));
        System.out.println(StrUtil.format("hp:{} mp:{}                 hp:{} mp:{}"
                , data.getInitiatorPetHp()
                , data.getInitiatorPetMp()
                , data.getOpponentPetHp()
                , data.getOpponentPetMp()));
        System.out.println("--------------------宠物对战message---------------------");

        BattlePet battlePet = new BattlePet();
        if (UserContext.getCurrentUser().equals(data.getInitiatorName())){
            //这里判断当前用户是否是战斗的发起者
            battlePet.setPetId(data.getInitiatorPetId());
            battlePet.setPetName(data.getInitiatorPetName());
            battlePet.setMp(data.getInitiatorPetMp());
            battlePet.setHp(data.getInitiatorPetHp());
        }else{
            battlePet.setPetId(data.getOpponentPetId());
            battlePet.setPetName(data.getOpponentPetName());
            battlePet.setMp(data.getOpponentPetMp());
            battlePet.setHp(data.getOpponentPetHp());
        }

        UserContext.setCurrentPet(battlePet);
        int choose;
        Scanner scanner = new Scanner(System.in);

        while (true){
            System.out.println("1.背包");
            System.out.println("2.切换宠物");
            System.out.println("3.使用技能");
            System.out.println("4.逃跑");
            choose = scanner.nextInt();
            scanner.nextLine();//删除换行符
            if (choose >= 1 && choose <= 4) {
                break;
            }
            System.out.println("无效选择，请重新输入。");
        }
        switch (choose) {
            case 1:
                new BattleRequestHandler().showBattleBagMenu(gameWebSocketClient);
                break;
            case 2:
                new BattleRequestHandler().showBattlePetMenu(gameWebSocketClient);
                break;
            case 3:
                new BattleRequestHandler().showBattleSkillMenu(gameWebSocketClient,data);
                break;
            case 4:
                System.out.println("你选择了逃跑..");
                //向服务端发送请求
                Result<String> request = new Result<>();
                request.setModuleName(ModuleTypeConstant.BATTLE);
                request.setCommand(SubmoduleTypeConstant.BATTLE_QUIT);
                request.setMessage(UserContext.getCurrentUser()+"逃跑了");
                request.setData(data.getInitiatorName().equals(UserContext.getCurrentUser())?data.getOpponentName():data.getInitiatorName());
                gameWebSocketClient.send(JSONUtil.toJsonStr(request));
                new BattleRequestHandler().showGameMenu(gameWebSocketClient);
                break;
            default:
                System.out.println("无效选择，请重新输入。");
                break;
        }
    }

    @Override
    public void showBattleBagMenu(GameWebSocketClient gameWebSocketClient, Result result) {
        if (UserContext.getCurrentUser().equals(result.getFromUser())){
            int choose;
            Scanner scanner = new Scanner(System.in);
            List<BagDTO>  battleBags = JSONUtil.toList(JSONUtil.toJsonStr(result.getData()), BagDTO.class);
            while (true){
                System.out.println("---------------背包 ("+UserContext.getCurrentUser()+")---------------");
                for (int i = 0; i < battleBags.size(); i++) {
                    BagDTO bagDTO = battleBags.get(i);
                    System.out.println(StrUtil.format(i+1+" 名称:{}，类型:{}，描述:{},数量:{}"
                            , bagDTO.getGoodsName()
                            , bagDTO.getType()
                            , bagDTO.getDescription()+" "+bagDTO.getValue()+"点"
                            , bagDTO.getQuantity()));
                }
                System.out.println("输入0返回");
                System.out.println("请输入您的选择: ");
                System.out.println("------------------背包-----------------");
                choose = scanner.nextInt();
                scanner.nextLine();//删除换行符
                if (choose >= 0 && choose <= battleBags.size() ) {
                    break;
                }
                System.out.println("无效选择，请重新输入。");
            }
            if (choose==0){
                //TODO 2024年11月15日20:13:53
                battleRequestService.doRound(gameWebSocketClient, result.getToUser());
//                showBattleBagMenu(gameWebSocketClient, result);
                System.out.println("等待对手响应。");
            }else{
                BagDTO bagDTO = battleBags.get(choose - 1);
                battleRequestService.useBag(gameWebSocketClient,result.getToUser(), bagDTO);
                System.out.println("等待对手响应。");
            }

        }



    }

    /**
     * 对于宠物的死亡的判断
     * @param gameWebSocketClient
     * @param result
     */
    @Override
    public void turnAurond(GameWebSocketClient gameWebSocketClient, Result result) {
        System.out.println(result.getMessage());
        if (result.getMessage().contains("死亡")){  //直接判断message中是否有死亡的关键字，如有，直接显示宠物菜单
            new BattleRequestHandler().showBattlePetMenu(gameWebSocketClient);
        }else {
            BattleRoundDTO data = JSONUtil.toBean(JSONUtil.toJsonStr(result.getData()), BattleRoundDTO.class);
            showBattleMenu(gameWebSocketClient, data);
        }
    }

    /**
     * 对战中显示宠物菜单
     * @param gameWebSocketClient
     * @param result
     */
    @Override
    public void showBattlePetMenu(GameWebSocketClient gameWebSocketClient, Result result) {
        List<BattlePet>  petList = JSONUtil.toList(JSONUtil.toJsonStr(result.getData()), BattlePet.class);
//        TODO 2024年11月15日15:14:32    petList.size()==0修改为petList.isEmpty()
        if (petList.isEmpty()){   //判断宠物库中是否还有宠物，没有则游戏结束
            System.out.println("没有宠物，游戏结束");
            Result<String> request = new Result<>();
            request.setModuleName(ModuleTypeConstant.BATTLE);
            request.setCommand(SubmoduleTypeConstant.BATTLE_QUIT);
            request.setMessage("对方死亡，you win!!!!!!!!!!!!");
            request.setData(UserContext.getCurrentUser());
            gameWebSocketClient.send(JSONUtil.toJsonStr(request));
            new BattleRequestHandler().showGameMenu(gameWebSocketClient);

        }else {

            int choose;
            Scanner scanner = new Scanner(System.in);

            while (true) {
                System.out.println("---------------宠物 ("+UserContext.getCurrentUser()+")---------------");
                for (int i = 0; i < petList.size(); i++) {
                    System.out.println(StrUtil.format("{}:{}", i + 1, petList.get(i).getPetName()));
                }
                System.out.println("输入0返回");
                System.out.println("请输入您的选择: ");
                System.out.println("-----------------宠物----------------");
                choose = scanner.nextInt();
                scanner.nextLine();//删除换行符
                if (choose >= 0 && choose <= petList.size()) {
                    break;
                }
                System.out.println("无效选择，请重新输入。");
            }
            if (choose == 0) {

                battleRequestService.doRound(gameWebSocketClient, result.getToUser());
                System.out.println("等待对手响应。");
            } else {
                BattlePet battlePet = petList.get(choose - 1);
                battleRequestService.changePet(gameWebSocketClient, result.getToUser(), battlePet);
                System.out.println("等待对手响应。");
            }
        }

    }

    /**
     * 显示技能菜单
     * @param gameWebSocketClient
     * @param result
     */
    @Override
    public void showBattleSkillMenu(GameWebSocketClient gameWebSocketClient, Result result) {
        int choose;
        Scanner scanner = new Scanner(System.in);
        List<Skill>  skillList = JSONUtil.toList(JSONUtil.toJsonStr(result.getData()), Skill.class);
        while (true){
            System.out.println("------------------技能 ("+UserContext.getCurrentUser()+")------------------");
            for (int i = 0; i < skillList.size(); i++) {
                Skill skill = skillList.get(i);
                System.out.println(StrUtil.format("{}:{}",i+1,skill.getName()+"\n需要mp:"+skill.getNeedMp()+"造成的伤害为: "+skill.getValue()));
            }
            //System.out.println("输入0返回");
            System.out.println("请输入您的选择: ");
            System.out.println("-------------------技能------------------");
            choose = scanner.nextInt();
            scanner.nextLine();//删除换行符
            if (choose >= 0 && choose <= skillList.size() ) {
                break;
            }
            System.out.println("无效选择，请重新输入。");
        }
        if (choose==0){
            battleRequestService.doRound(gameWebSocketClient, result.getToUser());
            System.out.println("等待对手响应。");
        }else{
            Skill skill = skillList.get(choose - 1);
            //直接在客户端判断技能所需要的mp值是否足够
            skill.setNeedMp(UserContext.getCurrentPet().getMp()-skill.getNeedMp());
            //不够则直接进入下一回合
            if (skill.getNeedMp()<0) {
                System.out.println("mp不足跳过");
                battleRequestService.doRound(gameWebSocketClient, result.getToUser());
                System.out.println("等待对手响应。");
            }else {

                skill.setId(UserContext.getCurrentPet().getPetId());
                battleRequestService.useSkill(gameWebSocketClient, skill);
                System.out.println("等待对手响应。");
            }
        }

    }




}
